/* s^2 - array of bits
   Copyright (C) 2018 Ariadne Devos

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef _sHT_BITVEC_H
#define _sHT_BITVEC_H

#include <stdint.h>
#include <sHT/test.h>

/** Test if a bit is set.

  @var{bits}: a bit array. Bits are numbered from the first byte to the last,
    from the least-significant bit to the most-significant.
  @var{index}: the index of the bit. It is within bounds.

  The bit should be set more often than it is not.
  No bounds checking is performed. This must be called in control position. */
__attribute__((always_inline))
__attribute__((pure))
static inline _Bool
sHT_bit_test(const uint8_t *bits, unsigned int index)
{
	/* x86 has a special instruction for testing a bit (bt), which GCC
	   doesn't generate automatically, but it is tricky to use -- as of
	   now, to no avail. There are tests for the courageous, in
	   <tests/bitvec.c>.

	   There seems to be no code size difference in
	   different choices of uintN_t. */
        return sHT_and_any(bits[index / 8], 1u << (index % 8));
}

#endif
